/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dal;

import java.sql.*;
import thaihung.common.SQLQuery;
/**
 *
 * @author bj
 */
public class DalBaiThi {

    public static int TaoBaiThi(String MaSV, String MaDeThi,String NgayThi) throws SQLException {
        SQLQuery sql = new SQLQuery();
        String lenh = "insert into BaiThi(MaSV,MaDeThi,NgayThi) values('" + MaSV + "','" + MaDeThi + "','" + NgayThi + "')";
        sql.Excute(lenh);
        lenh = "Select @@identity";
        int maBaiThi = sql.LayIdentity(lenh);
        return maBaiThi;
    }

    public static void TaoCauHoi_TraLoi(int maBaiThi, String maDeThi,Character[] dapAnChon) throws SQLException {
        SQLQuery sql = new SQLQuery();
        
        
        try
        {
            //Lay danh sach cau hoi theo ma de
            int[] dsMaCauHoi = new int[100];       
            int i = 0;
            String lenh = "select MaCauHoi from CauHoi_DeThi where MaDe='" + maDeThi + "' order by STT ASC";
            ResultSet rs = sql.ExcuteQR(lenh);
            while(rs.next())
            {
                dsMaCauHoi[i++] = rs.getInt(1);
            }
            
            //Tao danh sach cau tra loi
            for(int j = 0 ; j<i ; j++)
            {
                lenh = "insert into TraLoi values('" + maBaiThi + "'," + dsMaCauHoi[j] + ",'"+ dapAnChon[j] + "')";
                sql.Excute(lenh);
            }            
        }
        catch(SQLException ex)
        {
            throw ex;
        }
    }

    public static int XemKetQua(int maBaiThi, String maDeThi) throws SQLException {
        int soCauDung = 0;
        String[] dapAn = new String[100];
        String[] dapAnChon = new String[100];
        SQLQuery sql = new SQLQuery();
        
        try
        {    
            //Lay danh sach dap an
            int n = 0;
            String lenh = "select DapAn from CauHoi_DeThi ch_dt,CauHoi ch where ch_dt.MaCauHoi = ch.MaCauHoi and MaDe = '" + maDeThi + "' order by STT ASC";
            ResultSet rs = sql.ExcuteQR(lenh);
            while(rs.next())
            {
                dapAn[n++] = rs.getString(1);
            }
            
            //Lay danh sach dap an chon
            n = 0;
            lenh = "select DapAn from TraLoi where MaBaiThi = " + maBaiThi;
            rs = sql.ExcuteQR(lenh);
            while(rs.next())
            {
                dapAnChon[n++] = rs.getString(1);
            }
            
            //Kiem tra so cau dung
            for(int i = 0 ; i<n ; i++)
                if(dapAn[i].equals(dapAnChon[i]))
                    soCauDung++;
            return soCauDung;
        }
        catch(SQLException ex)
        {
            throw ex;
        }
        
    }
    
}
